package online.shenme.day13;

import java.util.concurrent.atomic.AtomicInteger;

public class TicketTest {
    public static void main(String[] args) {
        Ticket1 ticket = new Ticket1();
        for (int i = 0; i < 50; i++) {
            new Thread(ticket, "窗口"+i).start();
        }
//        new Ticket2("窗口1").start();
//        new Ticket2("窗口2").start();
//        new Ticket2("窗口3").start();
    }
}

class Ticket1 implements Runnable {
    private final AtomicInteger ai = new AtomicInteger(50);

    @Override
    public void run() {
        while (ai.get() > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "\t剩余：" + ai.decrementAndGet());
        }
    }
}

class Ticket2 extends Thread {
    private static int num = 50;
    static Object lock = new Object();

    public Ticket2() {
    }

    public Ticket2(String name) {
        super(name);
    }

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                if (num > 0) {
                    num--;
                    System.out.println(Thread.currentThread().getName() + "\t剩余：" + num);
                } else {
                    break;
                }
            }
        }
    }
}